﻿using System;
using AspectCore.DynamicProxy;
using AspectCore.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace AspectCore.Extensions.DependencyInjection
{
    [NonAspect]
    public class ServiceContextProviderFactory : IServiceProviderFactory<IServiceContext>
    {
        /// <summary>
        /// 由IServiceCollection创建IServiceContext
        /// </summary>
        /// <param name="services">服务容器</param>
        /// <returns>aspectcore提供的服务容器</returns>
        public IServiceContext CreateBuilder(IServiceCollection services)
        {
            return services.ToServiceContext();
        }

        /// <summary>
        /// 由IServiceContext创建IServiceProvider
        /// </summary>
        /// <param name="contextBuilder">IServiceProviderFactory的泛型参数，由此对象构建IServiceProvider</param>
        /// <returns>依赖注入容器</returns>
        public IServiceProvider CreateServiceProvider(IServiceContext contextBuilder)
        {
            return contextBuilder.Build();
        }
    }
}